package com.controller;

import com.service.bookService;
import com.service.orderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OrderInfoServlet extends HttpServlet {
    private orderService orderService=new orderService();
    private bookService bookService=new bookService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String orderid = req.getParameter("od");
        String userid = req.getParameter("us");
         if(userid==null ||userid.equals("")){
             return;
         }
        int uid = Integer.parseInt(userid);
        try {
            ResultSet resultSet = orderService.queryOneOrder(uid, orderid);
            if(resultSet!=null){
                out.print("<table border=\"1\">");
                out.print("<tr><td colspan=\"5\" align=\"center\">您的订单信息为：</td></tr>");
                out.print("<tr><td>图书编号</td><td>图书名</td><td>购买数量</td><td>价格</td><td>购买日期</td></tr>");
                String str = "";
                double res=0.0;
                while (resultSet.next()){
                    out.print("<tr>");
                    out.print("<td>"+resultSet.getString(3)+"</td>");
                    String bookName = bookService.getBookName(resultSet.getString(3));
                    out.print("<td>"+bookName+"</td>");
                    out.print("<td>"+resultSet.getInt(4)+"</td>");
                    out.print("<td>"+resultSet.getDouble(5)+"</td>");
                    res+=resultSet.getDouble(5);
                    out.print("<td>"+resultSet.getDate(6)+"</td>");
                    if(resultSet.getInt(7)==0){
                        str="未收货";
                    }else{
                        str="已收货";
                    }
                    out.print("</tr>");
                }
                out.print("</table>");
                out.print("总共:"+res+"元<br/>");

                //确认收货
                out.print("<a href=\"receiveServlet?od="+orderid+"&us="+userid+"\">确认收货</a><br/>");


//                out.print("<h3 style=\"color: red\">订单号为:"+orderid+"("+str+")</h3><br/>");
                out.print("当前订单状态：<span style=\"color: red\">"+str+"</span>");
            }



        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
